package service;

import dao.BookMapper;
import dao.CategoryMapper;
import dao.ShoppingMapper;
import dao.UserMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import pojo.*;
import until.SqlSessionFactoryUtils;

import java.util.List;
import java.util.Random;

public class SearchIndexpagere {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
    public int adminUpdateCategory(String id,String sort){
        //获取Session
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        CategoryMapper mapper = sqlSession.getMapper(CategoryMapper.class);
        int i =  mapper.adminUpdateCategory(id, sort);
        sqlSession.commit();
        return i;

    }
    public int adminDeleteBook(String id){
        //获取Session
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        int i = mapper.adminDeleteBook(id);
        sqlSession.commit();
        return i;
    }
    public List<Category> adminCategoryList(){
        //获取Session
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        CategoryMapper mapper = sqlSession.getMapper(CategoryMapper.class);
        return mapper.adminselectAll();
    }

    public int adminUpdateBook(String id ,String name ,double price){
        //获取Session
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        int i = mapper.adminUpdateBook(id, name, price);
        sqlSession.commit();
        return i ;

    }
    public List<Book> selectRandomBook(){
        //获取Session
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        List<Book> books = mapper.selectRandomIndex();
        return books;
    }
    public List<Book> selectClassBook(String bookType, int page){
        //获取Session
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        List<Book> books = mapper.selectClassBook(bookType,page);
        return books;
    }
    public List<Book2> selectClassBook2(String bookType){
        //获取Session
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        List<Book2> books = mapper.selectClassBook2(bookType);
        return books;
    }
    public List<Book> selectBook(String id,int page){
        //获取Session
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        List<Book> books = mapper.selectBook(id,page);
        return books;
    }
    public Page selectClassBookPage(String bookType){
        //获取Session
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        return mapper.selectClassBookPage(bookType);
    }

    public Page selectBookPage(String name){
        //获取Session
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        return mapper.selectBookPage(name);
    }
    public Book selectBookDetail(String id){
        //获取Session
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        return mapper.selectBookDetail(id);
    }

    public List<Book2> selectBookList(){
        //获取Session
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
        return mapper.selectBookList();
    }
//    public List<Book> selectShopping(String username){
//        //获取Session
//        SqlSession sqlSession = factory.openSession();
//        //获取Mapper
//        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
//        return mapper.selectShopping(username);
//    }
    public List<Shopping> selectShopping(String username){
        //获取Session
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        ShoppingMapper mapper = sqlSession.getMapper(ShoppingMapper.class);
        return mapper.selectShopping(username);
    }

    public int addShopping(String bookId,String username){
        //获取Session
        SqlSession sqlSession = factory.openSession();
        BookMapper mapper1 = sqlSession.getMapper(BookMapper.class);
        UserMapper mapper2 = sqlSession.getMapper(UserMapper.class);
        User user = mapper2.selectUserId(username);
        Book book = mapper1.selectBookDetail(bookId);
        int i = mapper1.addShopping(user.getId(), book.getName(), book.getAuthor(), book.getPrice(), book.getImageName(),bookId);
        System.out.println("插入数据返回的值为"+i);
        // 手动提交事务
        sqlSession.commit();
        return i;
    }
//    public int deleteShopping(long id){
//        SqlSession sqlSession = factory.openSession();
//        sqlSession.getMapper(BookMapper)
//    }
//    public int insertShopping(String username){
//        //获取Session
//        SqlSession sqlSession = factory.openSession();
//        //获取Mapper
//        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
////        return mapper.selectShopping(username);
//        //往order中插入一条数据
//
//    }
//    public int deleteShopping(String username){
//        //获取Session
//        SqlSession sqlSession = factory.openSession();
//        //获取Mapper
//        BookMapper mapper = sqlSession.getMapper(BookMapper.class);
////        return mapper.selectShopping(username);
//
//    }



}
